<?php
/**
 * @author by bilee.
 * 时间: 17/1/22 下午11:53
 * PC版接口
 */
namespace Api\Controller;

use Common\Controller\HomebaseController;

class PcController extends HomebaseController
{

    /**
     * PC端发送验证码
     * 新建表 sms_log
     */
    function get_tel_code()
    {
        if (!checkToken(I('r'))) {
            return RET_NO_V(401, "令牌错误，非法访问！");
        }
        //return RET_NO_V(200, "短信发送成功！");
        if (pc_get_sms(I('phone'))) {
            return RET_NO_V(200, "短信发送成功！");
        } else {
            return RET_NO_V(400, "短信发送失败，请稍后再试！");
        }
    }

    /**
     * PC端登录验证
     */
    function user_login()
    {
        $sms_log = M("sms_log");
        //查询最后条短信验证码
        $ret = $sms_log->where(array('sms_send_phone' => I('phone'),'is_used'=>0))->order('sms_time desc')->limit(1)->find();
        //判断验证码的时长
        $time = (int)(strtotime($ret['sms_time']) - strtotime(date('Y-m-d H:i:s'))) / 60 * -1;
        //是否是已经失效
        if (round($time) >= 15) {
            return RET_NO_V(401, "抱歉，验证码已经失效，请重新发送！");
        }else{
            if($ret['sms_code'] === I('code')){
                $app_users = M("app_users");
                //标识已经被使用
                $sms_log->where(array('sms_send_phone' => I('phone'),'is_used'=>0,'sms_code'=>I('code')))->save(array('is_used'=>1));
                $userinfo = $app_users->where(array('mobile'=>I('phone')))->find();
                session('userinfo',$userinfo);
                return RET_NO_V(200,"恭喜，登录成功！");
            }else{
                return RET_NO_V(402,"抱歉，验证码验证失败！请重试！");
            }
        }
    }


}